package com.mycomp.cm.interceptors;

import java.util.Calendar;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class MyTimeInterceptor extends HandlerInterceptorAdapter {

	private int timeToAllow;

	public void setTimeToAllow(int timeToAllow) {
		this.timeToAllow = timeToAllow;
		System.out.println("Time To Allow:"+timeToAllow);
	}

	@Override
	public boolean preHandle(HttpServletRequest req, HttpServletResponse resp,
			Object arg2) throws Exception {
		
		System.out.println("Interceptor Pre Handle");

		int chr = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
		System.out.println("Current Time:" + chr);

		if (chr > timeToAllow) {
			resp.sendRedirect("timeUP.jsp");
			return false;
		} else {
			return true;
		}
	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
			Object arg2, ModelAndView arg3) throws Exception {

	}

	@Override
	public void afterCompletion(HttpServletRequest arg0,
			HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {

	}

}
